﻿package  
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.utils.getTimer;
	import Resource.*;
	import Util.*;
	
	/**
	 * ...
	 * @author Lissa Avery
	 */
	public class Map extends Sprite 
	{
		
		private var world:World;
		public static const TILE_SIZE:int = 32;
		public static const SECTOR_SIZE:int = 64;
		
		// We draw to this while drawing the map, then blit it all to the Sprite in one chunk.
		private var drawBuffer:BitmapData;
		
		public function Map() 
		{
			world = World.getWorld();
			draw();
			
		}
		
		public function draw():void {
			initialRender();
			
		}
		
		private function initialRender():void {
			var start:int = getTimer();
			
			drawBuffer = new BitmapData(world.getWidth() * SECTOR_SIZE, world.getHeight() * SECTOR_SIZE);
			
			for (var i:int = 0; i < world.getWidth(); i++) {
				for (var j:int = 0; j < world.getHeight(); j++) {
					Tile.WORLD_TILES.copyFrame(world.getBiome(i, j), Utility.randomNumber(0, 2), 
						drawBuffer, i * SECTOR_SIZE, j * SECTOR_SIZE);
					horizontalInterpolation(i, j);
					verticalInterpolation(i, j);
					diagonalInterpolation(i, j);
				}
			}
			
			graphics.clear();
			NamatjiraGraphics.drawSprite(graphics, drawBuffer, 0, 0);
			
			trace(getTimer() - start);
		}
		
		/**
		 * Looks forward to interpolate the intervening tile.
		 * @param	x
		 * @param	y
		 */
		private function horizontalInterpolation(x:int, y:int):void {
			var nextX:int = (x + world.getWidth() + 1) % world.getWidth();
			var currentBiome:int = world.getBiome(x, y);
			var nextBiome:int = world.getBiome(nextX, y);
			
			var xPosition:int = x * SECTOR_SIZE + TILE_SIZE;
			var yPosition:int = y * SECTOR_SIZE;
			
			if (world.getBiome(x, y) == world.getBiome(nextX, y)) {
				Tile.WORLD_TILES.copyFrame(world.getBiome(x, y), Utility.randomNumber(0, 2), 
						drawBuffer, xPosition, yPosition);
			} else if (world.getBiome(x, y) < world.getBiome(nextX, y)) {
				// Then I want a joint tile betwixt the two.
				Tile.getComboTile(currentBiome, nextBiome).copyFrame(1, 0, 
						drawBuffer, xPosition, yPosition);
			} else {
				Tile.getComboTile(nextBiome, currentBiome).copyFrame(1, 1,
						drawBuffer, xPosition, yPosition);
			}
		}
		
		/**
		 * Looks down to interpolate the intervening tile.
		 * @param	x
		 * @param	y
		 */
		private function verticalInterpolation(x:int, y:int):void {
			var nextY:int = (y + world.getHeight() + 1) % world.getHeight();
			var currentBiome:int = world.getBiome(x, y);
			var nextBiome:int = world.getBiome(x, nextY);
			
			var xPosition:int = x * SECTOR_SIZE;
			var yPosition:int = y * SECTOR_SIZE + TILE_SIZE;
			
			if (world.getBiome(x, y) == world.getBiome(x, nextY)) {
				Tile.WORLD_TILES.copyFrame(world.getBiome(x, y), Utility.randomNumber(0, 2),
					drawBuffer, xPosition, yPosition);
			} else if (world.getBiome(x, y) < world.getBiome(x, nextY)) {				
				Tile.getComboTile(currentBiome, nextBiome).copyFrame(2, 3,
					drawBuffer, xPosition, yPosition);
			} else {
				Tile.getComboTile(nextBiome, currentBiome).copyFrame(2, 2,
					drawBuffer, xPosition, yPosition);
			}
		}
		
		/**
		 * Looks down and to the right to interpolate the intervening tile.
		 * @param	x
		 * @param	y
		 */
		private function diagonalInterpolation(x:int, y:int):void {
			var nextX:int = (x + world.getWidth() + 1) % world.getWidth();
			var nextY:int = (y + world.getHeight() + 1) % world.getHeight();
			
			var xPosition:int = x * SECTOR_SIZE + TILE_SIZE;
			var yPosition:int = y * SECTOR_SIZE + TILE_SIZE;
			
			var biomes:Array = [[world.getBiome(x, y), world.getBiome(nextX, y)], 
				[world.getBiome(x, nextY), world.getBiome(nextX, nextY)]];
			
			Tile.copyDiagonalTile(biomes, drawBuffer, xPosition, yPosition);
		}
		
	}
	
}